﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ItemControl
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Person> _peoples;
        public ObservableCollection<Person> Peoples
        { 
            get { return _peoples; }
            set
            {
                _peoples = value;
                OnPropertyChange();
            }
        }

        private Status _status;
        public Status Status
        {
            get { return _status; }
            set
            {
                _status = value;
                OnPropertyChange();
            }
        }

        public RelayCommand AddCommand { get; set; }
        public MainWindowViewModel()
        {
            Peoples=new ObservableCollection<Person>();
            Person person = new Person() { Name = "zs", Age = 20,status=Status.Error };
            Person person1 = new Person() { Name = "ls", Age = 40,status=Status.Normal };
            Peoples.Add(person);
            Peoples.Add(person1);

            AddCommand = new RelayCommand(() =>
            {
                Peoples[0].status++;
                Peoples[1].status++;
            });
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        public void OnPropertyChange([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class Person : INotifyPropertyChanged
    { 
        private string _name;
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChange();
            }
        }
        private int _age;
        public int Age
        {
            get => _age;
            set
            {
                _age = value;
                OnPropertyChange();
            }
        }
        private Status _status;
        public Status status
        {
            get => _status;
            set
            {
                _status = value;
                OnPropertyChange();
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        public void OnPropertyChange([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        private Action action;

        public RelayCommand(Action action)
        {
            this.action = action;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            action?.Invoke();
        }
    }
}
